package com.sifeng.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @ClassName RedPacketUtils
 * @Description 生成随机红包
 * @Author yang_gsh
 * @Date 2019/8/6 17:53
 **/
public class RedPacketUtils {
    public static void main(String[] args) {
        List<Integer> integerList = randomPackage(1000000, 1, 1, 888);
        //integerList.forEach(integer -> System.out.println(integer));
        int sum = integerList.stream().mapToInt(value -> {
           return value.intValue();
        }).sum();


        integerList.stream().filter((a)->a>1).forEach(System.out::println);
        System.out.println(integerList.stream().filter((a) -> a > 1).mapToInt(a -> a.intValue()).count());
        System.out.println("-------------");
        System.out.println("total:"+sum);

    }

    /**
     * 生成随机红包（可剩余）
     * @param totalAmount 总金额，以分为单位
     * @param totalNumber 总个数
     * @param minAmountPer 每个可分配最小金额，以分为单位
     * @param maxAmountPer 每个可分配最大金额，以分为单位
     * @return java.util.List<java.lang.Integer>  分配的金额列表
     **/
    public static List<Integer> randomPackage(int totalAmount, int totalNumber, int minAmountPer, int maxAmountPer) {
        List<Integer> resultList = new ArrayList<>();
        int allocationNumber = totalNumber + 1;
        for (int i = 0; i < allocationNumber; i++) {
            //保护值
            int protectionValue = (allocationNumber - i - 1) * minAmountPer;
            //可支配金额
            int disposableAmount = totalAmount - protectionValue;
            if(disposableAmount < maxAmountPer) {
                maxAmountPer = disposableAmount;
            }
            int result = restRound(minAmountPer, maxAmountPer, i, disposableAmount, allocationNumber - 1);
            totalAmount = totalAmount - result;
            if(i < allocationNumber - 1) {
                resultList.add(result);
            }
        }

        return resultList;
    }

    private static int restRound(int min, int max, int i, int money, int count) {
        int redpac = 0;
        if (i == count) {
            redpac = money;
        } else {
            redpac = (new Random().nextInt(max - min) + min);
        }
        return redpac;
    }
}
